

export default {
	namespaced: true,
	state: {
		token: uni.getStorageSync('token') || '',
		openid: uni.getStorageSync('h5openid') || '',
		wxMiniOpenId: uni.getStorageSync('wxxcxopenid') || '',   //微信小程序openid
		userinfo: JSON.parse(uni.getStorageSync('userinfo') || '{}'),
		looks: [],
		collects: [], //不持久化
	},
	mutations: {
		setToken(state, token) {
			try {
				uni.setStorageSync('token', token)
			} catch (e) {
				//TODO handle the exception
			}
			state.token = token
		},
		clearToken(state) {
			try {
				uni.removeStorageSync('token')
			} catch (e) {
				console.log('error')
			}
			state.token = null
		},
		setOpenId(state, data) {
			uni.setStorageSync('h5openid', data)
			state.openid = data
		},
		setWxMiniOpenId(state, data) {
			uni.setStorageSync('wxxcxopenid', data)
			state.wxMiniOpenId = data
		},
		clearOpenId(state) {
			try {
				uni.removeStorageSync('h5openid')
			} catch (e) {
				console.log('error')
			}
			state.openid = ''
		},

		setUserInfo(state, data) {
			try {
				uni.setStorageSync('userinfo', JSON.stringify(data))
			} catch (e) {
				console.log('用户数据错误')
			}
			state.userinfo = data;
		},
		setLooks(state, data) {
			state.looks = data
		},
		setCollects(state, data) {
			state.collects = data
		}
	},
	actions: {
		updateUserinfo({ commit }, newUserInfo) {
			// 这里可以包含异步操作
			commit('setUserinfo', newUserInfo);
		}
	}
}
